package org.dtp;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;

import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author Bestlee
 * @description: 主要对nacos配置文件进行监听
 * @Version 1.0
 */
public class NacosListener implements Listener, InitializingBean {
    @NacosInjected
    private ConfigService service;

    /*这个方法看源码的解释是为了执行receiveConfigInfo方法的线程*/
    @Override
    public Executor getExecutor() {
        return Executors.newFixedThreadPool(1);
    }

    @Override
    public void receiveConfigInfo(String configInfo) {
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ByteArrayResource(configInfo.getBytes()));
        /*这里是打印的配置信息 {dtp.executor[0].core-pool-size=5, dtp.executor[0].maximum-pool-size=50, dtp.executor[0].pool-name=yyp, dtp.executor[1].core-pool-size=100, dtp.executor[1].pool-name=ls}11111111
         */
        Properties properties = yamlPropertiesFactoryBean.getObject();
        /*将获取的信息赋值到dtpProperties上*/
        DtpProperties dtpProperties = new DtpProperties();
        MapConfigurationPropertySource configurationProperty = new MapConfigurationPropertySource(properties);
        Binder binder = new Binder(configurationProperty);
        ResolvableType resolvableType = ResolvableType.forClass(DtpProperties.class);
        Bindable<Object> target = Bindable.of(resolvableType).withExistingValue(dtpProperties);
        binder.bind("dtp", target);
        /*获取到对应的线程池并赋值*/
        for (DtpProperties.DtpExecutorProperties dtpExecutorProperties : dtpProperties.getExecutor()) {
            DtpExecutor dtpExecutor = DtpUtil.getDtpExecutor(dtpExecutorProperties.getPoolName());
            dtpExecutor.setCorePoolSize(dtpExecutorProperties.getCorePoolSize());
            dtpExecutor.setMaximumPoolSize(dtpExecutorProperties.getMaximumPoolSize());
        }

    }

    /**
     * @description: 监听nacos配置文件, 这块具体可以参考下spring创建bean的流程 https://www.cnblogs.com/tuyang1129/p/12861617.html
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        /**
         *  nacos config
         * dtp:
         *  executor:
         *    - pool-name: yyp
         *      core-pool-size: 5
         *      maximum-pool-size: 50
         *    - pool-name: ls
         *      core-pool-size: 10
         *      maximum-pool-size: 50
         */
        service.addListener("dtp.yaml", "DEFAULT_GROUP", this);
    }
}
